﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChungNhanSinhVien.Entities;
using ChungNhanSinhVien.Infrastructures;
using System.Web.Mvc;

namespace ChungNhanSinhVien.Models
{
    public class LoaiChungNhanRepository
    {
        Web_ChungNhanSinhVienEntities data;
        public LoaiChungNhanRepository()
        {
            //if (data == null)
                data = new Web_ChungNhanSinhVienEntities();
        }
        public List<LoaiChungNhan> LayTatCaLoaiChungNhan()
        {
            var list = new List<LoaiChungNhan>();
            list = (from m in data.LoaiChungNhans
                        select m).ToList();
            return list;
        }

        public List<SelectListItem> LayTatCaLoaiChungNhanSelectList()
        {
            var list = (from m in data.LoaiChungNhans
                        select m).ToList()
                            .Select(slt => new SelectListItem(){
                            Text = slt.TenLoaiChungNhan,
                            Value = slt.ID.ToString()
                        }).ToList();
            return list;
        }

        public int LayIDTheoTenLoaiChungNhan(string tenLoaiCN)
        {
            var kq = (from m in data.LoaiChungNhans
                      where m.TenLoaiChungNhan.Equals(tenLoaiCN)
                      select m.ID).FirstOrDefault();
            return kq;
        }

        public bool TaoMoi(LoaiChungNhan cn)
        {
            if (cn != null)
            {
                data.AddToLoaiChungNhans(cn);
                data.SaveChanges();
                return true;
            }
            return false;
        }

        public bool CapNhat(LoaiChungNhan cn)
        {
            if (cn != null)
            {
                var kq = (from m in data.LoaiChungNhans
                          where m.ID.Equals(cn.ID)
                          select m).FirstOrDefault();
                if (kq != null)
                {
                    kq.TenLoaiChungNhan = cn.TenLoaiChungNhan;
                    kq.MoTa = cn.MoTa;
                    data.SaveChanges();                    
                    return true;
                }
            }
            return false;
        }

        public bool Xoa(int id, out int count)
        {
            count = 0;
            if (id != 0)
            {
                count = new ChungNhanRepository().DemChungNhansTheoLoaiCNID(id);
                if (count.Equals(0))
                {
                    var cn = LayDoiTuongTheoID(id); 
                    data.DeleteObject(cn);
                    data.SaveChanges();
                    return true;
                }
            }
            return false;
        }

        public LoaiChungNhan LayDoiTuongTheoID(int id)
        {
            var kq = (from m in data.LoaiChungNhans
                      where m.ID.Equals(id)
                      select m).FirstOrDefault();
            return kq;
        }

        public bool KiemTraTonTaiTheoTen(string ten, int loaicnID)
        {
            var kq = (from m in data.LoaiChungNhans
                      where m.TenLoaiChungNhan.Equals(ten)
                      && !m.ID.Equals(loaicnID)
                      select m.ID).FirstOrDefault();
            if (kq != 0)
                return true;
            return false;
        }
    }
}